package com.fast.fast.common.core.config.kaptcha;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.Properties;

/**
 * Kaptcha自动配置类
 *
 * @author lyf
 * @date 2022/01/01 00:00 周六
 **/
@RequiredArgsConstructor
@EnableConfigurationProperties({KaptchaProperties.class})
@AutoConfiguration
public class KaptchaConfig {

    private final KaptchaProperties kaptchaProperties;

    @Bean
    public DefaultKaptcha defaultKaptcha() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.setProperty("kaptcha.border", kaptchaProperties.getBorder().getEnable());
        properties.setProperty("kaptcha.border.color", kaptchaProperties.getBorder().getColor());
        properties.setProperty("kaptcha.textproducer.font.color", kaptchaProperties.getText().getFont().getColor());
        properties.setProperty("kaptcha.textproducer.font.size", kaptchaProperties.getText().getFont().getSize());
        properties.setProperty("kaptcha.textproducer.font.names", kaptchaProperties.getText().getFont().getNames());
        properties.setProperty("kaptcha.textproducer.char.length", kaptchaProperties.getText().getAChar().getLength());
        properties.setProperty("kaptcha.textproducer.char.space", kaptchaProperties.getText().getAChar().getSpace());
        properties.setProperty("kaptcha.image.width", kaptchaProperties.getImage().getWidth());
        properties.setProperty("kaptcha.image.height", kaptchaProperties.getImage().getHeight());
        properties.setProperty("kaptcha.obscurificator.impl", kaptchaProperties.getImage().getType());
        properties.setProperty("kaptcha.noise.impl", kaptchaProperties.getImage().getNoNoise());
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }


}
